$(document).ready(OnDocumentReady);

function OnDocumentReady()
{
	EditableTextInit($('#sheet-caption input'), UpdateSheetName);
	$('#sheet-caption input').autoGrowInput({
		comfortZone: 50, 
		minWidth: 200, 
		maxWidth: 2000
	});
}

function key()
{
	return $('#hdn-key').val();
}

function UpdateSheetName()
{
	var newName = $('#sheet-caption input').val();
	$.ajax({
		url: 'sheetedit/setname?key=' + key() + '&newname=' + encodeURIComponent(newName), 
		success: OnSheetNameUpdateSuccess, 
		error: OnSheetNameUpdateFail
	});
}

function OnSheetNameUpdateSuccess()
{
	//"saving..." disappears
}

function OnSheetNameUpdateFail()
{
	alert('fail(((');
}

function OnAddItemClick()
{
	$.ajax({
		url: 'sheetedit/additem?key=' + key(),
		success: OnNewItemAdded,
		error: OnNewItemAddFail
	});
}

function OnNewItemAdded(req, stat)
{
	var key = req.responseText;
	var sItemEl = '<div class="sheetitem" key="' + key + '"><span></span><input type="text"/></div>'
	var itemElement = $(sItemEl); 
	itemElement.children('input').addClass('active');
	$('div #sheet-items').append(itemElement);
}

function OnNewItemAddFail(a, b)
{
	alert(a.responseText);
}
